package dao.impl;

import dao.BaseDao;
import dao.EmployeeDao;
import entity.Employee;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeImpl extends BaseDao implements EmployeeDao {
    @Override
    public List<Employee> findAll() {
        List<Employee> employees = new ArrayList<Employee>();
        Connection conn = getConnection();
        System.out.println("数据库是否连接成功："+(conn != null? "成功":"失败"));
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from employee";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if (rs!=null) {
                while (rs.next()) {
                    Employee employee = new Employee();
                    employee.setId(rs.getInt("id"));
                    employee.setName(rs.getString("name"));
                    employee.setPassword(rs.getString("password"));
                    employee.setImage(rs.getString("image"));
                    employee.setBirthday(rs.getString("birthday"));
                    employee.setSalary(rs.getString("salary"));
                    employee.setDepId(rs.getInt("depId"));
                    employee.setCreateTime(rs.getString("createTime"));
                    employee.setUpdateTime(rs.getString("updateTime"));
                    employee.setStart(rs.getInt("start"));
                    employees.add(employee);
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(rs);
        }
        return employees;
    }

    @Override
    public List<Employee> findByName(String name) {
        List<Employee> employees = new ArrayList<>();
        Connection conn = getConnection();
        String sql = "select * from employee where name like concat('%',?,'%')";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            rs = ps.executeQuery();
            if (rs!=null) {
                while (rs.next()) {
                    Employee employee = new Employee();
                    employee.setId(rs.getInt("id"));
                    employee.setName(rs.getString("name"));
                    employee.setPassword(rs.getString("password"));
                    employee.setImage(rs.getString("image"));
                    employee.setBirthday(rs.getString("birthday"));
                    employee.setSalary(rs.getString("salary"));
                    employee.setDepId(rs.getInt("depId"));
                    employee.setCreateTime(rs.getString("createTime"));
                    employee.setUpdateTime(rs.getString("updateTime"));
                    employee.setStart(rs.getInt("start"));
                    employees.add(employee);
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close(rs);
        }
        return employees;
    }
}
